home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-06 | 5.6 KB | 280 lines | [TEXT/KAHL] |
- /***
- *
- * MacBob.cp - The Bob programming language as a BBEdit extension.
- * Copyright © 1995 by Christopher E. Hyde. All rights reserved.
- *
- ***/
-
- // THINK C
- #include <SetupA4.h>
- #include <Balloons.h>
- #include <GestaltEqu.h>
-
- #include "CSavePort.h"
- #include "Exceptions.h"
-
- // BBEdit
- #include "BBEdit.h"
- #include "DialogUtilities.h"
-
- #include "Bob.h"
-
- // Global Variables
- BBCallbackPtr BBEdit;
- CIStream gInput;
- COStream gOutput;
- TPrefs gPrefs;
-
- // Local Variables
- static Handle pHandles = nil;
-
- // Local Functions
- pascal void main (BBCallbackPtr callbacks, WindowPeek w);
- static short MainDialog (void);
- static void DoAboutBox (void);
- static void SetDialogFontAndSize (DialogPtr d, short fontNum, short fontSize);
- static void DoStopAlert (short msgID);
- static void CleanUp (void);
-
-
- pascal void
- main (BBCallbackPtr callbacks, WindowPeek w)
- {
- RememberA0();
- SetUpA4();
-
- BBEdit = callbacks;
-
- if (BBEdit->version < 2) {
- DoStopAlert(errBBEditVersionNotHighEnough);
- goto finish;
- }
-
- if (!w && w->windowKind != userKind) {
- DoStopAlert(errWrongWindowKind);
- goto finish;
- }
-
- #if 0
- long selStart, selEnd, firstChar;
- BBEdit->GetSelection(&selStart, &selEnd, &firstChar);
- if (selStart == selEnd) {
- DoStopAlert(errNeedSelection);
- goto finish;
- }
- #endif
-
- short prefsSize;
- BBEdit->GetPreference(kPrefsResType, sizeof(gPrefs), &gPrefs, &prefsSize);
-
- // If we couldn't get the prefs, set them to the default
- if (prefsSize <= 0) {
- for (short i = kFirstOption; i < kLastOptionPlus1; ++i)
- _Opt(i) = false;
- Opt(PatchCode) =
- Opt(BufferStdErr) = true;
- }
-
- if (MainDialog() == kRun) {
- BBEdit->SetPreference(kPrefsResType, sizeof(gPrefs), &gPrefs, &prefsSize);
-
- pHandles = nil;
- gInput.fHandle =
- gOutput.fHandle = nil;
-
- TRY
- gInput.Open(&w->port);
- gOutput.Open(!Opt(BufferStdErr));
-
- FailNil(BBEdit->NewDocument());
-
- // Do real Bob stuff
- BobMain();
- CATCH
- if (gFailE == memFullErr)
- gFailE = errNotEnoughMemory;
- if (gFailE < 0)
- BBEdit->ReportOSError(gFailE);
- #if 0
- else if (gFailE < 0)
- gFailE = errUnknown;
- DoStopAlert(gFailE);
- #else
- if (gFailE > errUnknown)
- SysBeep(0);
- else
- DoStopAlert(gFailE);
- #endif
- ENDTRY
-
- CleanUp();
- }
-
- finish:
- RestoreA4();
- }
-
-
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-
- #if 0
- void*
- Malloc (UInt32 size)
- {
- return Calloc(size, 1);
- }
- #endif
-
-
- #if 0
- static int callocCount = 0;
- #endif
-
- void*
- Calloc (UInt32 count, UInt32 size)
- {
- Handle h = BBEdit->Allocate(count * size + sizeof(Handle), true);
- #if 0 && qDebug
- if (Opt(Debug))
- PrintErrF("\t• Calloc(%d, %d) => 0x%X\r",
- count, size, (h == nil) ? nil : &(*h)[sizeof(Handle)]);
- #endif
- // ++callocCount;
- FailNil(h);
- HLockHi(h);
- **(Handle**) h = pHandles;
- pHandles = h;
- return &(*h)[sizeof(Handle)];
- }
-
-
- extern "C" void __malloc_cleanup(void);
-
-
- static void
- CleanUp (void)
- {
- #if 0
- for (Handle h = pHandles; callocCount-- && h != nil; ) {
- PrintErrF("DisposeHandle(0x%X)\r", h);
- #endif
-
- TRY
- for (Handle nextH, h = pHandles; h != nil; h = nextH) {
- nextH = **(Handle**) h;
- DisposeHandle(h);
- }
-
- gInput.Close();
- gOutput.Close();
- CATCH
- #if qDebug
- BBEdit->ReportOSError(gFailE);
- #endif
- ENDTRY
- }
-
-
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-
-
- static short
- MainDialog (void)
- {
- bool saveBalloonStatus, hasHelpMgr = false;
- long answer;
- if (Gestalt(gestaltHelpMgrAttr, &answer) == noErr) {
- hasHelpMgr = true;
- saveBalloonStatus = HMGetBalloons();
- }
-
- DialogPtr d = BBEdit->CenterDialog(rOptionsDlog);
- CSavePort savePort(d);
-
- InitKeyEquiv(d, rOptionsDlog);
- SetupUserItem(d, kLine, BBEdit->FrameDialogItem);
-
- SetItemEnable(d, kBalloonHelp, hasHelpMgr);
-
- for (short i = kFirstOption; i < kLastOptionPlus1; ++i)
- SetItemValue(d, i, _Opt(i));
-
- short item;
- do {
- ModalDialog(KeyEquivFilter, &item);
-
- if (item == kAbout)
- DoAboutBox();
- else if (item == kBalloonHelp) {
- bool balloons = !HMGetBalloons();
- HMSetBalloons(balloons);
- SetItemIcon(d, kBalloonHelp, balloons ? rHelpIconOn : rHelpIconOff);
- } else if (item >= kFirstOption && item < kLastOptionPlus1)
- _Opt(item) = ToggleItem(d, item);
-
- } while (item != kRun && item != kCancel);
-
- if (hasHelpMgr)
- HMSetBalloons(saveBalloonStatus);
-
- EndKeyEquiv(d);
- DisposeDialog(d);
- return item;
- }
-
-
- static void
- DoAboutBox (void)
- {
- DialogPtr d = BBEdit->CenterDialog(rAboutDlog);
-
- if (d != nil) {
- CSavePort savePort(d);
- SetDialogFontAndSize(d, geneva, 9);
- SetupUserItem(d, kAboutLine, BBEdit->FrameDialogItem);
- short item;
- ModalDialog(BBEdit->StandardFilter, &item);
- DisposeDialog(d);
- }
- }
-
-
- // SetDialogFontAndSize is a slightly modified version of an original alt.sources.mac code snippet by Leonard Rosenthol.
- static void
- SetDialogFontAndSize (DialogPtr d, short fontNum, short fontSize)
- {
- // set up the port info
- TextFont(fontNum);
- TextSize(fontSize);
- //SetDialogFont(fontNum);
-
- // now deal with the static & edit text issues
- FontInfo f;
- GetFontInfo(&f);
- (*DialogPeek(d)->textH)->txFont = fontNum;
- (*DialogPeek(d)->textH)->txSize = fontSize;
- (*DialogPeek(d)->textH)->lineHeight = f.ascent + f.descent + f.leading;
- (*DialogPeek(d)->textH)->fontAscent = f.ascent;
- }
-
-
- static void
- DoStopAlert (short msgID)
- {
- Str255 msg;
- GetIndString(msg, rErrorStrings, msgID);
-
- SysBeep(2);
- DialogPtr d = BBEdit->CenterDialog(rStopAlertDlog);
- if (d == nil)
- return;
-
- CSavePort savePort(d);
- ParamText(msg, nil, nil, nil);
- short item;
- ModalDialog(BBEdit->StandardFilter, &item);
- DisposeDialog(d);
-
- // aShort = StopAlert(alertID, filterProc);
- }
-